package com.coolham.switter.db;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class UserData extends AbstractTable {
	final static String TAG = "Switter_DB";
	
	final static String TABLE_NAME = "users";
	final static String ID = "_id";
	final static String SCREEN_NAME = "screen_name";
	final static String PASSWD = "passwd";
	final static String TOKEN = "token";
	final static String TOKEN_SECRET = "token_secret";

	
	public UserData(Context ct) {
		super(ct);
	}
	
	/**
	 *      
	 */
	public void createTable() {
		if(bCreated)
			return;
		
		synchronized(lock) {
			if(bCreated)
				return;
			
			String createSQL = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( " + 
				ID + " integer primary key, " + 
				SCREEN_NAME + " text, " +
				PASSWD + " text, " +
				TOKEN + " text, " +
				TOKEN_SECRET + " text " +
				");";
		
			super.createTable(createSQL);
			bCreated = true;
		}
	}

	public boolean isTableExits() {
		return super.isTableExits(TABLE_NAME);
	}

	public void close() {
		super.close();
	}

	public Cursor query() {
		String[] columns = {ID, SCREEN_NAME, PASSWD,  TOKEN, TOKEN_SECRET};

		SQLiteDatabase db = mConn.getConnection();
		Cursor c = db.query(TABLE_NAME, columns, null, null, null, null, null);

		return c;
	}

	public int insertOrUpdate(int userId, String screenName, String token,
			String tokenSecret) {
		
		int ret = -1;
		if(isExist(userId))
			ret = update(userId, screenName, token, tokenSecret);
		else
			ret = insert(userId, screenName, token, tokenSecret);

		return ret;
	}
	
	public int update(int userId, String screenName, String token,
			String tokenSecret) {
		
		ContentValues values = new ContentValues(); 
		values.put(ID, userId);
		values.put(SCREEN_NAME, screenName);
		values.put(TOKEN, token);
		values.put(TOKEN_SECRET, tokenSecret);
		
		String whereClause = "_id=?";
		String[] whereArgs = { Integer.toString(userId) };
		SQLiteDatabase db = mConn.getConnection();
		int ret = db.update(TABLE_NAME, values, whereClause, whereArgs);
		return ret;
	}

	public int insert(int userId, String screenName, String token,
			String tokenSecret) {
		
		ContentValues values = new ContentValues(); 
		values.put(ID, userId);
		values.put(SCREEN_NAME, screenName);
		values.put(TOKEN, token);
		values.put(TOKEN_SECRET, tokenSecret);
		
		SQLiteDatabase db = mConn.getConnection();
		long ret = db.insert(TABLE_NAME, null, values);
		return ret>0 ? 1 : 0;
	}

	private boolean isExist(int userId) {
		String[] columns = {ID };
		String selection =  ID + "=?" ;
		String[] selectionArgs = { Integer.toString(userId) };
		
		SQLiteDatabase db = mConn.getConnection();
		Cursor c = db.query(TABLE_NAME, columns, selection, selectionArgs, null, null, null);
		int n = c.getCount();
		c.close();
		return n>0? true : false;
	}

	public HashMap<String, Object> queryByName(String screenName) {
		String[] columns = { ID, SCREEN_NAME, PASSWD, TOKEN, TOKEN_SECRET };

		SQLiteDatabase db = mConn.getConnection();
		Cursor c = db.query(TABLE_NAME, columns, null, null, null, null, null);

		ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();

		HashMap<String, Object> map = null;

		if (c.moveToFirst()) {
			int id = c.getInt(0);
			map = new HashMap<String, Object>();
			map.put("id", id);
			map.put(SCREEN_NAME, c.getString(1));
			map.put(TOKEN, c.getString(3));
			map.put(TOKEN_SECRET, c.getString(4));
			list.add(map);
		}

		c.close();
		return map;
	}
	
}
